package com.finley.flowabledemo.controller;


import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: xianfei.chao
 * @date 2025/6/30 16:51
 * @version: 1.0
 */
@RestController
@RequestMapping("/agv")
public class MockAgvRunningController {

    private static Deque<String> INT_QUEUE = new ArrayDeque<>();

    private static final String AGV_CODE = "3001";

    private static boolean IS_ASC = true;

//    @PostConstruct
//    public void init() {
//        fillQueue();
//    }


    private void fillQueue() {
        INT_QUEUE.offer("199844XY216375");
        INT_QUEUE.offer("201209XY216375");
        INT_QUEUE.offer("201209XY215205");
        INT_QUEUE.offer("201209XY213775");
        INT_QUEUE.offer("201209XY212475");
        INT_QUEUE.offer("201209XY211305");
        INT_QUEUE.offer("201209XY209875");
        INT_QUEUE.offer("201209XY208575");
        INT_QUEUE.offer("201209XY207275");
        INT_QUEUE.offer("201209XY205975");
        //
        INT_QUEUE.offer("201209XY204500");
    }

    public void fillReverseQueue() {
        // 返回去
        INT_QUEUE.offer("201209XY205975");
        INT_QUEUE.offer("201209XY207275");
        INT_QUEUE.offer("201209XY208575");
        INT_QUEUE.offer("201209XY209875");
        INT_QUEUE.offer("201209XY211305");
        INT_QUEUE.offer("201209XY212475");
        INT_QUEUE.offer("201209XY213775");
        INT_QUEUE.offer("201209XY215205");
        INT_QUEUE.offer("201209XY216375");
    }


    @GetMapping("/running")
    public ResponseEntity running() {
        Map<String, String> map = new HashMap<>();
        if (INT_QUEUE.isEmpty()) {
            if (IS_ASC) {
                fillQueue();
            } else {
                fillReverseQueue();
            }
        }
        if (!INT_QUEUE.isEmpty()) {
            String value = INT_QUEUE.poll();
            map.put(AGV_CODE, value);
        }
        if (INT_QUEUE.isEmpty()) {
            IS_ASC = !IS_ASC;
        }
        return ResponseEntity.ok(map);
    }


}
